import { post } from '../../../../utils/request.js'
import global from '../../../../utils/global.js'
var WxParse = require('../../../../wxParse/wxParse.js');
let that
Page({
  data: {
    userId: wx.getStorageSync("useInfo").id,
    order:[]
  },

  onLoad: function (options) {
    console.log(options)
    that = this;
    that.orderDetail(options.orderId, options.orderDetailType);
    that.setData({
     typenew: options.orderDetailType,
      noPay: options.nopay
    })
    let pages = getCurrentPages();
    let prevPage = pages[pages.length - 2];
    prevPage.setData({  // 将我们想要传递的参数在这里直接setData。上个页面就会执行这里的操作。
      prev: false,
    })
  },
//  获取订单详情
  orderDetail(id, orderDetailType){
     let param ={
       orderId:id,
       orderDetailType: orderDetailType*1,
       userId:that.data.userId
      //  userId: "1738"
     }
    post(global.apiUrl.orderDetail, param, function (res) {
      console.log(res.obj.orderProductList)
      if (res.obj.orderProductList[0].detail ==null){
        WxParse.wxParse('article', 'html', "暂无信息", that, 5)
      }else{
        WxParse.wxParse('article', 'html', res.obj.orderProductList[0].detail, that, 5)
      }
      
      that.setData({
        orderDetail: res.obj,
        order: res.obj.orderProductList
      })

    })
  },
  // 取消订单
  cancelOrder(e) {
    console.log(e.currentTarget.dataset.id)
    let param = {
      orderId: e.currentTarget.dataset.id,
      userId: that.data.userId
      // userId: "1738"
    }
    wx.showModal({
      title: '提示',
      content: '您确定要取消订单吗？',
      success: function (res) {
        if (res.confirm) {
          post(global.apiUrl.cancelOrder, param, function (res) {
            if (res.msg == "取消成功") {
              wx.showToast({
                title: '订单取消',
                icon: 'succes',
                duration: 3000,
                mask: true
              })
              setTimeout(function () {
                wx.navigateBack({
                  delta: 1
                })
              }, 2000)
            }
          })
        }
      }
    })

  },
  // 支付
  wechatAppletPay() {
    console.log()
    let order = that.data.order
    let code = "";
    let payId = that.data.order[0].payId
    wx.login({
      success: function (res) {
        if (res.code) {
          //发起网络请求  
          console.log(res.code,)
          code = res.code;
          console.log(code, "zaoqij")
          that.isPayInfo(code, payId, order)
        } else {
          wx.showModal({
            title: '提示',
            content: '获取用户信息失败！',
          })
          console.log('获取用户登录态失败！' + res.errMsg)
        }
      }
    })
  },
  isPayInfo(code, payId, obj) {
    console.log(obj)
    let param = {
      code: code,
      payId: payId
    }
    post(global.apiUrl.wechatAppletPay, param, function (res) {
      console.log(res)
      that.wxPayOrder(res.obj, obj, payId)
    })
  },
  // 支付接口
  wxPayOrder: function (result, obj, payId) { //去微信支付-小程序
    let resultVal = result
    wx.requestPayment({
      "appId": resultVal.appId,
      "nonceStr": resultVal.nonceStr,
      "signType": resultVal.signType,
      "package": resultVal.pkg,
      "timeStamp": resultVal.timeStamp + '',//数值转化为字符串
      "paySign": resultVal.paySign,
      'success': function (res) {
        wx.hideLoading()
        wx.reLaunch({
          url: "/pages/pay/paySuccess/paySuccess?pay=1&order=" + JSON.stringify(obj) + "&payId=" + payId
        })

      },
      'fail': function (res) {
        console.log(res)
        wx.showModal({
          title: '提示',
          content: '支付失败',
          success: function () {
            wx.reLaunch({
              url: "/pages/pay/paySuccess/paySuccess?pay=0&order=" + JSON.stringify(obj) + "&payId=" + payId
            })
          },//接口调用成功
          fail: function () {
            wx.reLaunch({
              url: "/pages/pay/paySuccess/paySuccess?pay=0&order=" + JSON.stringify(obj) + "&payId=" + payId
            })
          },  //接口调用失败的回调函数  
        })


      },
      'complete': function (res) { }
    })
  },
})